package com.yonyou.pmclouds.workstartreport.process;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.CheckItemConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.workstartreport.entity.WorkStartCheckVO;
import com.yonyou.pmclouds.workstartreport.entity.WorkStartVO;
import com.yonyou.pmclouds.workstartreport.mapper.WorkStartCheckMapper;
import com.yonyou.pmclouds.workstartreport.mapper.WorkStartMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.UUID;

public class WorkStartInsertProcessor implements SingleProcessor<WorkStartVO> {

    private WorkStartCheckMapper workStartCheckMapper;
    private WorkStartMapper mapper;
    //利用构造方法赋值
    public WorkStartInsertProcessor(WorkStartCheckMapper workStartCheckMapper,WorkStartMapper mapper){
        this.workStartCheckMapper=workStartCheckMapper;
        this.mapper=mapper;
    }
    @Override
    public void process(WorkStartVO vo, WorkStartVO orig) throws BusinessException {
        //获取检查项对象数组
        WorkStartCheckVO[] workStartCheckVOS = vo.getWorkStartCheckVOS();
        //遍历数组
        for(WorkStartCheckVO c:workStartCheckVOS){
            //获取检查项序号，供排序用
            String code = mapper.selectCode(c.getPkWorkqualitydetail());
            //添加租户
            c.setPkTenant(vo.getPkTenant());
            //添加序号，用于展示排序
            c.setCheckcode(code);
            //为检查项生成主键
            c.setPkCheck(UUID.randomUUID().toString());
            //为检查项添加创建者，个人觉得这个字段不用存在
            BeanHelper.setProperty(c, CommonFieldConst.CREATOR, RuntimeEnvironment.getLoginUserId());
            //为检查项添加创建时间，个人觉得这个字段不用存在
            BeanHelper.setProperty(c, CommonFieldConst.CREATIONTIME, new Date(System.currentTimeMillis()));
            //判断是否存在图片或者备注，有图片为2，无图有留言为1，都无为0
            if(c.getPictures()!=null && c.getPictures().length>0){
                c.setLogo(CheckItemConst.LOGO_ALL_PROSSESS);
            }else if(!StringUtils.isBlank(c.getInstruction())){
                c.setLogo((CheckItemConst.LOGO_NO_PICTURE));
            }
            else{
                c.setLogo(CheckItemConst.LOGO_NO_DATA);
            }
            //添加单据主键，为查询检查项时，依靠单据主键以及条件主键来定位
            c.setPkWorkstartreport(vo.getPkWorkstartreport());
            //添加检查项，调用BaseMapper中的insert方法，其会根据传入的WorkStartCheck对象的属性自动生成对应的insert语句。
            workStartCheckMapper.insert(c);
        }

    }
}
